package com.example.dragandview.recyclerviewplus;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.example.dragandview.R;
import com.example.dragandview.recyclerviewplus.adapters.GridViewAdapter;
import com.example.dragandview.recyclerviewplus.adapters.ListViewAdapter;
import com.example.dragandview.recyclerviewplus.adapters.RecyclerViewBaseAdaptar;
import com.example.dragandview.recyclerviewplus.adapters.StaggerAdapter;
import com.example.dragandview.recyclerviewplus.beans.Datas;
import com.example.dragandview.recyclerviewplus.beans.ItemBean;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class RecyclerviewActivity extends AppCompatActivity {
    private RecyclerView rv_pro;
    private List<ItemBean> mData;
    private RecyclerViewBaseAdaptar mAdapter;
    private SwipeRefreshLayout mRefreshLayout;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.recyclerview_pro);
        rv_pro = findViewById(R.id.rv_pro);
        mRefreshLayout =  this.findViewById(R.id.refresh_layout);
        //准备数据
        /**
         * 准备数据,一般来说,我们在现实开发中,我们的数据是从网络中获取的,这里面只是演示。
         * 所以我们只是模拟数据,在现实开发中也是要模拟数据的,比如说,后台没有准备好的时候。
         */
        initData();
        //设置默认的显示样式为ListView的效果
        showList(true, false);
        //处理下啦刷新
        handlerDownPullUpdate();
    }

    private void handlerDownPullUpdate() {

        mRefreshLayout.setColorSchemeResources(R.color.colorPrimary, R.color.colorPrimaryDark, R.color.colorAccent);
        mRefreshLayout.setEnabled(true);
        mRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                //在这里面去执行刷新数据的操作
                /**
                 * 当我们在顶部,下拉的时候 ,这个方法就会被出发
                 * 但是,这个方法是MainThread是主线程,不可以执行耗时操作。
                 * 一般来说,我们去请求数据在开一个线程去获取
                 * //这里面演示的话,我直接添加一条数据
                 */
                //添加数据
                ItemBean data = new ItemBean();
                data.title = "我是新添加的数据...";
                data.icon = R.mipmap.pic_09;
                mData.add(0, data);
                //更新UI
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        //这里要做两件事,一件是让刷新停止,另外一件则是要更新列表
                        mAdapter.notifyDataSetChanged();
                        mRefreshLayout.setRefreshing(false);
                    }
                }, 3000);
            }
        });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.recyclerpro_menu,menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int itemid = item.getItemId();
        switch (itemid){
            case R.id.grid_vertial_normal:
                Log.e("TAG","grid_vertial_normal");
                showGrid(true,false);
                break;
            case R.id.grid_vertial_inverse:
                showGrid(true,true);
                break;
            case R.id.grid_hor_normal:
                showGrid(false,false);
                break;
            case R.id.grid_hor_inverse:
                showGrid(false,true);
                break;

            case R.id.list_vertical_normal:
                showList(true, false);
                break;
            case R.id.list_vertical_inverse:
                showList(true, true);
                break;
            case R.id.list_hor_normal:
                showList(false, false);
                break;
            case R.id.list_hor_inverse:
                showList(false, true);
                break;

            case R.id.wrap_vertical_normal:
                showStagger(true,false);
                break;
            case R.id.wrap_vertical_inverse:
                showStagger(true,true);
                break;
            case R.id.wrap_hor_normal:
                showStagger(false,false);
                break;
            case R.id.wrap_hor_inverse:
                showStagger(false,true);
                break;
            // 多种条目类型被点击了
            case R.id.more_type:

                //跳到一个新的Activity里面去实现这个功能
                Intent intent = new Intent(this, MoreTypeActivity.class);
                startActivity(intent);
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * 这个方法用于模拟数据
     */
    private void initData() {
        //List<DataBea>---->Adapter------>setAdapter------>显示数据。
        //创建数据集合
        mData = new ArrayList<>();

        //创建模拟数据
        for (int i = 0; i < Datas.icons.length; i++) {
            //创建数据对象
            ItemBean data = new ItemBean();
            data.icon = Datas.icons[i];
            data.title = "我是第 " + i + " 个条目";
            //添加到集合里头
            mData.add(data);
        }

    }
    /**
     * 这个方法用于实现和GridView一样的效果
     */
    private void showGrid(boolean isVertical, boolean isReverse) {

        //创建布局管理器
        GridLayoutManager layoutManager = new GridLayoutManager(this, 3);
        //设置水平还是垂直
        layoutManager.setOrientation(isVertical ? GridLayoutManager.VERTICAL : GridLayoutManager.HORIZONTAL);
        //设置标准(正向)还是反向的
        layoutManager.setReverseLayout(isReverse);

        //设置布局管理器
        rv_pro.setLayoutManager(layoutManager);

        //创建适配器
        mAdapter = new GridViewAdapter(mData);

        //设置适配器
        rv_pro.setAdapter(mAdapter);
        initListener();
    }

    /**
     * 这个方法用于现实ListView一样的效果
     */
    private void showList(boolean isVertical, boolean isReverse) {
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        //设置布局管理器来控制
        //设置水平还是垂直
        linearLayoutManager.setOrientation(isVertical ? LinearLayoutManager.VERTICAL : LinearLayoutManager.HORIZONTAL);
        //设置标准(正向)还是反向的
        linearLayoutManager.setReverseLayout(isReverse);
        rv_pro.setLayoutManager(linearLayoutManager);

        mAdapter = new ListViewAdapter(mData);
        rv_pro.setAdapter(mAdapter);
        initListener();
    }

    /**
     * 这个方法用于实现瀑布流的效果
     */
    private void showStagger(boolean isVertical, boolean isReverse) {
        //准备布局管理器
        StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(2, isVertical ? StaggeredGridLayoutManager.VERTICAL : StaggeredGridLayoutManager.HORIZONTAL);
        //设置布局管理器的方向
        layoutManager.setReverseLayout(isReverse);

        //设置布局管理器到RecyclerView里
        rv_pro.setLayoutManager(layoutManager);

        //创建适配器
        mAdapter = new StaggerAdapter(mData);

        //设置适配器
        rv_pro.setAdapter(mAdapter);
        initListener();

    }

    private void initListener(){
        //listview的点击事件回调
        /*
        ListView lv = new ListView(this);
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            }
        });
        */
        mAdapter.setOnItemClickListener(new RecyclerViewBaseAdaptar.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                //这里处理条目的点击事件,该干嘛就干嘛,跳转的就跳转...
                Toast.makeText(RecyclerviewActivity.this, "您点击的是第" + position + "个条目", Toast.LENGTH_SHORT).show();
            }
        });


        //这里面去处理上拉加载更多
        if (mAdapter instanceof ListViewAdapter) {
            ((ListViewAdapter) mAdapter).setOnRefreshListener(new ListViewAdapter.OnRefreshListener() {
                @Override
                public void onUpPullRefresh(final ListViewAdapter.LoaderMoreHolder loaderMoreHolder) {
                    //这里面去加载更多的数据,同样,需要在子线程中完成,这里仅作演示

                    //更新UI
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {

                            Random random = new Random();

                            if (random.nextInt() % 2 == 0) {
                                ItemBean data = new ItemBean();
                                data.title = "我是新添加的数据...";
                                data.icon = R.mipmap.pic_09;
                                mData.add(data);

                                //这里要做两件事,一件是让刷新停止,另外一件则是要更新列表
                                mAdapter.notifyDataSetChanged();
                                loaderMoreHolder.update(loaderMoreHolder.LOADER_STATE_NORMAL);
                            } else {
                                loaderMoreHolder.update(loaderMoreHolder.LOADER_STATE_RELOAD);
                            }

                        }
                    }, 3000);
                }
            });
        }
    }
}
